package com.example.fragload;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import com.example.fragload.fragments.SecondFragment;
import com.example.fragload.fragments.FirstFragment;
import com.example.fragload.fragments.ThirdFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class StatePageAdapterActivity extends AppCompatActivity {
    @BindView(R.id.nav_view)
    BottomNavigationView navView;
    @BindView(R.id.vp_host_fragment)
    ViewPager viewPager;
    List<Fragment> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_viewpage);
        ButterKnife.bind(this);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
//        ft.addToBackStack(null);
        list.add(new FirstFragment());
        list.add(new SecondFragment());
        list.add(new ThirdFragment());

        FragmentStatePagerAdapter fragmentPagerAdapter = new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }

            @Override
            public int getCount() {
                return list.size();
            }
        };
        viewPager.setAdapter(fragmentPagerAdapter);
//        viewPager.setOffscreenPageLimit(2); //预加载剩下两页

        navView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case R.id.navigation_user:
                        viewPager.setCurrentItem(0);
                        return true;
                    case R.id.navigation_room:
                        viewPager.setCurrentItem(1);
                        return true;
                    case R.id.navigation_setting:
                        viewPager.setCurrentItem(2);
                        return true;
//                        return NavigationUI.onNavDestinationSelected(menuItem, navController);
                }
                return false;
            }
        });
    }
}
